﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using UcAsp.Models;
namespace UcAsp.Controllers
{
    public class InfoManagerController : Controller
    {
        //
        // GET: /InfoManager/
        private UcAspNetEntities entities = new UcAspNetEntities();
        public ActionResult Index()
        {
            if (Session["Info"] == null)
            {

                return Redirect("/adminlogin/login");
            }
            else
            {
                if (Session["Info"].ToString() != "Admin")
                {
                    return Redirect("/adminlogin/login");
                }
            }
            return View();
        }
        public ActionResult VideoManager()
        {
            if (Session["Info"] == null)
            {

                return Redirect("/adminlogin/login");
            }
            else
            {
                if (Session["Info"].ToString() != "Admin")
                {
                    return Redirect("/adminlogin/login");
                }
            }
            List<Articles> list = new List<Articles>();
            var query = from c in entities.Articless
                        where c.CategorieID == 15
                        orderby c.ID descending
                        select c;

            int pagesize = 10;
            int pageindex = 0;
            if (Request.QueryString["page"] != null)
            {
                pageindex = int.Parse(Request.QueryString["page"].ToString()) - 1;
            }
            ViewData["PageIndex"] = pageindex + 1;
            ViewData["PageCount"] = (query.Count() + 9) / 10;

            list = query.Skip(pageindex * pagesize).ToList<Articles>().Take(pagesize).ToList();




            return View(list);
        }

        public ActionResult OtherManager()
        {
            if (Session["Info"] == null)
            {

                return Redirect("/adminlogin/login");
            }
            else
            {
                if (Session["Info"].ToString() != "Admin")
                {
                    return Redirect("/adminlogin/login");
                }
            }
            List<Articles> list = new List<Articles>();
            var query = from c in entities.Articless
                        where c.CategorieID == 53
                        orderby c.ID descending
                        select c;

            int pagesize = 10;
            int pageindex = 0;
            if (Request.QueryString["page"] != null)
            {
                pageindex = int.Parse(Request.QueryString["page"].ToString()) - 1;
            }
            ViewData["PageIndex"] = pageindex + 1;
            ViewData["PageCount"] = (query.Count() + 9) / 10;

            list = query.Skip(pageindex * pagesize).ToList<Articles>().Take(pagesize).ToList();




            return View(list);
        }
        public ActionResult UserManager()
        {
            if (Session["Info"] == null)
            {

                return Redirect("/adminlogin/login");
            }
            else
            {
                if (Session["Info"].ToString() != "Admin")
                {
                    return Redirect("/adminlogin/login");
                }
            }
            int pagesize = 10;
            int pageindex = 0;
            if (Request.QueryString["page"] != null)
            {
                pageindex = int.Parse(Request.QueryString["page"].ToString()) - 1;
            }
            var query = entities.UserInfos;
            ViewData["PageIndex"] = pageindex + 1;
            ViewData["PageCount"] = (query.Count() + 9) / 10;
            List<UserInfo> list = query.OrderByDescending(o => o.ID).Skip(pageindex * pagesize).Take(pagesize).ToList();
            return View(list);
            
        }
        public ActionResult AskManager()
        {
            if (Session["Info"] == null)
            {

                return Redirect("/adminlogin/login");
            }
            else
            {
                if (Session["Info"].ToString() != "Admin")
                {
                    return Redirect("/adminlogin/login");
                }
            }
            List<Articles> list = new List<Articles>();
            var query = from c in entities.Articless
                        where c.CategorieID == 17
                        orderby c.ID descending
                        select c;

            int pagesize = 10;
            int pageindex = 0;
            if (Request.QueryString["page"] != null)
            {
                pageindex = int.Parse(Request.QueryString["page"].ToString()) - 1;
            }
            ViewData["PageIndex"] = pageindex + 1;
            ViewData["PageCount"] = (query.Count() + 9) / 10;

            list = query.Skip(pageindex * pagesize).ToList<Articles>().Take(pagesize).ToList();
            return View(list);
        }
        public ActionResult CommentsManager()
        {
            int pagesize = 10;
            int pageindex = 0;
            if (Request.QueryString["page"] != null)
            {
                pageindex = int.Parse(Request.QueryString["page"].ToString()) - 1;
            }
            var query = entities.Commentss.Where(o => o.ParentID == 1);
            ViewData["PageIndex"] = pageindex + 1;
            ViewData["PageCount"] = (query.Count() + 9) / 10;
            List<Comments> list = query.OrderByDescending(o=>o.ID).Skip(pageindex * pagesize).Take(pagesize).ToList();
            return View(list);
        }
        public ActionResult ReplyComment()
        {
            int id = int.Parse(Request.QueryString["ID"].ToString());
            Comments comments = entities.Commentss.FirstOrDefault(o=>o.ID==id);
            List<Comments> list = entities.Commentss.Where(o => o.ParentID == id).ToList();
            ViewData["Comments"] = list;
            return View(comments);

        }
        public ActionResult ShowComments()
        {
            if (Session["Info"] == null)
            {

                return Redirect("/adminlogin/login");
            }
            else
            {
                if (Session["Info"].ToString() != "Admin")
                {
                    return Redirect("/adminlogin/login");
                }
            }
            int id = int.Parse(Request.Form["ID"]);
            bool release = bool.Parse(Request.Form["Release"]);
            Comments model = entities.Commentss.FirstOrDefault(o => o.ID == id);
            model.IsRelease = release == true ? false : true;
            entities.SaveChanges();
           
            Message message = new Message { Result = true, Messages = "" };
            return Json(message, JsonRequestBehavior.AllowGet);
        }
         [ValidateInput(false)]
        public ActionResult SaveComments()
        {
            int id = int.Parse(Request.Form["ID"]);
            string content = Request.Form["PublichContent"];
            Comments model = new Comments();
            model.ParentID = id;
            model.PublichContent = content;
            model.PublicDate = DateTime.Now;
            model.ReplyDateTime = DateTime.Now;
            model.IsRelease = true;
            model.UserID = 999999;
            model.UserName = "悠点";
            entities.Commentss.Add(model);
            entities.SaveChanges();
            Message message = new Message { Result = true, Messages = "" };
            return Json(message, JsonRequestBehavior.AllowGet);
        }
         public ActionResult DelComments()
         {
             int id = int.Parse(Request.Form["ID"]);
            
             Comments model = entities.Commentss.FirstOrDefault(o => o.ID == id);
             entities.Commentss.Remove(model);
             entities.SaveChanges();
             Message message = new Message { Result = true, Messages = "" };
             return Json(message, JsonRequestBehavior.AllowGet);
         }
         public ActionResult ShowUser()
         {
             if (Session["Info"] == null)
             {

                 return Redirect("/adminlogin/login");
             }
             else
             {
                 if (Session["Info"].ToString() != "Admin")
                 {
                     return Redirect("/adminlogin/login");
                 }
             }
             int id = int.Parse(Request.QueryString["ID"]);
             UserInfo userinfo = entities.UserInfos.FirstOrDefault(o => o.ID == id);
             if (userinfo == null)
             {
                 userinfo = new UserInfo();
             }
             return View(userinfo);
         }
         public ActionResult SaveUser()
         {
             if (Session["Info"] == null)
             {

                 return Redirect("/adminlogin/login");
             }
             else
             {
                 if (Session["Info"].ToString() != "Admin")
                 {
                     return Redirect("/adminlogin/login");
                 }
             }
             int id = int.Parse(Request.Form["ID"].ToString());
             string userpassowrd = Request.Form["UserPassword"].ToString();
             int point = int.Parse(Request.Form["Point"].ToString());
             UserInfo userinfo = entities.UserInfos.FirstOrDefault(o => o.ID == id);
             userinfo.Point = point;
             userinfo.UserPassword = userpassowrd;
             entities.SaveChanges();
              Message message = new Message { Result = true, Messages = "" };
             return Json(message, JsonRequestBehavior.AllowGet);

         }
    }
   
}
